import 'dart:math';

import 'package:fijkplayer/fijkplayer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/ijkplayer/iplayer.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/pages/news/main/models/news.dart';
import 'package:industry_net/pages/news/main/views/content/controller.dart';
import 'package:industry_net/pages/news/news_detail/index.dart';

class VideoView extends StatelessWidget {
  VideoView({Key? key, required this.mod}) : super(key: key);
  final NewsModel mod;
  final controller = Get.find<NewsDetailController>();
  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(circle4),
      child: FijkView(
        width: Get.width - 2 * gap12,
        height: 200.r,
        player: IPlayer.instance.player,
        color: ColorsUtil.white,
        fit: FijkFit.ar16_9,

        cover: const NetworkImage(
            'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GIF'),
        onDispose: (p0) {
          IPlayer.instance.reset();
        },
        // panelBuilder: (player, data, context, viewSize, texturePos) {
        //   return CustomFijkPanel(
        //       player: player,
        //       buildContext: context,
        //       viewSize: viewSize,
        //       texturePos: texturePos);
        // },
      ),
    );
  }
}

class PreparedView extends StatelessWidget {
  PreparedView({Key? key, required this.mod}) : super(key: key);
  final controller = Get.find<ContentController>();
  final NewsModel mod;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        children: [
          loadImage(
            url: 'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GIF',
            fit: BoxFit.contain,
          ),
          Positioned(
              child: Center(
            child: IconButton(
                onPressed: () {
                  // controller.play(index: 5, mod: mod);
                },
                icon: Icon(
                  Icons.play_circle,
                  size: 40.r,
                  color: ColorsUtil.white,
                )),
          )),
        ],
      ),
    );
  }
}

class CustomFijkPanel extends StatefulWidget {
  final FijkPlayer player;
  final BuildContext buildContext;
  final Size viewSize;
  final Rect texturePos;

  const CustomFijkPanel({
    required this.player,
    required this.buildContext,
    required this.viewSize,
    required this.texturePos,
  });

  @override
  _CustomFijkPanelState createState() => _CustomFijkPanelState();
}

class _CustomFijkPanelState extends State<CustomFijkPanel> {
  FijkPlayer get player => widget.player;
  bool _playing = false;
  bool _isReading = false;
  @override
  void initState() {
    super.initState();
    widget.player.addListener(_playerValueChanged);
  }

  void _playerValueChanged() {
    FijkValue value = player.value;
    if (value.state == FijkState.asyncPreparing ||
        value.state == FijkState.initialized) {
      if (!_isReading) {
        setState(() {
          _isReading = true;
        });
      }
    } else if (_isReading) {
      setState(() {
        _isReading = false;
      });
    }

    // if (value.state == FijkState.started) {
    //   if (playing != _playing) {
    //   setState(() {
    //     _playing = playing;
    //   });
    // }
    // }

    bool playing = (value.state == FijkState.started);
    if (playing != _playing) {
      setState(() {
        _playing = playing;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    Rect rect = Rect.fromLTRB(
        max(0.0, widget.texturePos.left),
        max(0.0, widget.texturePos.top),
        min(widget.viewSize.width, widget.texturePos.right),
        min(widget.viewSize.height, widget.texturePos.bottom));

    return Positioned.fromRect(
      rect: rect,
      child: Container(
        alignment: Alignment.center,
        child: _isReading
            ? const RefreshProgressIndicator()
            : IconButton(
                icon: Icon(
                  _playing ? Icons.pause_circle : Icons.play_circle,
                  color: Colors.white,
                  size: 40.r,
                ),
                onPressed: () {
                  if (IPlayer.instance.player.state == FijkState.idle) {
                    IPlayer.instance.play(
                        'https://sample-videos.com/video123/flv/240/big_buck_bunny_240p_10mb.flv',
                        autoPlay: true);
                  } else {
                    _playing ? widget.player.pause() : widget.player.start();
                  }
                },
              ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    IPlayer.instance.stop();
    player.removeListener(_playerValueChanged);
  }
}
